package com.pine.dcts.utils;

import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AESUtil {

    static String key = "U0CgGQFpJt2ZpGuR";  //16位
    static String iv = "gzAbpbVaSRA0zLZV";  //16位

    public static void main(String args[]) throws Exception {
        System.out.println(encryptAES("{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}{\"ZRRXX\":\"{\"ZRR_XM\":\"崔晓宇\",\"ZRR_SWJG_MC\":\"章丘\",\"ZRR_SJLY\":\"01\",\"ZRR_SJHM\":\"15588505678\",\"ZRR_SWJG_DM\":\"13701910000\",\"ZRR_ZJHM\":\"372922199309213232\",\"ZRR_DJXH\":\"1101122343333\"}"));

        String mw = "h5VN1mzTDhd8vjBkJDNUiM6qc7KOUg4+STx6ScWmiHz051j9YSj2w41+xsXs0Q4kdpCONZd11H4x ks/YuDPtKAtfRPjouJcR4SFRqP25qRCzKu4U2QmSPmVJP4vIv3tsyVnG0lyE+AFGzSKP2f0Uz6K/ N0YHLtjTxNwzyjk/uQ+/EZTjHCelywzVSlqS2pc3JxrQFRJ0vJEwWNHM8hHPUQybLI6Fu9inZo2m ANoZFb6uVcDSCIH98rYEmjJB5Fo8BBcjceNo5vrXjaV3YwjJlBnCS3SZsg6yNBOur3SvVuy6d1q5 +rGmyIgc/7v91qAXqwKi37+dY/y96BebNvO+TJSicJBhLgFgW+N2gRH7Ya50gEBynEtFJ1nLZdMG 4r6Eu1v87+i153xtiDdh6hEvaZo/ovsXuaif014CHL0qMbpY64g7mBv1LWn2BPSH0urFa4sS8GPL b174tTZcR0qNG9X2gOhO7JDlPzPwPaY3MddrNh6zPn4TDe7XIkl8EYxVGZ7KqdG/a8+YsIyCxeIX eSdeV81Hnak0UPmK8YRpiNi3WrzirOKZ7DmOwTywKG+CEP7WJKdgrmDEGoMaXKJNUthwCwbhlgZw lbrWkCnJ8ppKOjBD7Sd79+ZS91qLIb+ggrxGhqckIMqryHFHfbJ1OPhES8zoJ+C6ZJHR3OpOpWDz 78/RtlpBW/vkstObf/30q83G7RuvH13Cecg/ShOxfO4/ETNJfOij+Pbmc5Hsj8BxkiW1/LwBg4LO zoKUpVQx";


        mw = mw.replaceAll(" ", "");

        System.out.println(decryptAES(encryptAES("123456")));

        System.out.println("AES解密后的密文："+decryptAES(mw));
    }

    public static String encryptAES(String data) throws Exception {

        try {

            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");   //参数分别代表 算法名称/加密模式/数据填充方式
            int blockSize = cipher.getBlockSize();

            byte[] dataBytes = data.getBytes();
            int plaintextLength = dataBytes.length;
            if (plaintextLength % blockSize != 0) {
                plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
            }

            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);

            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes());

            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);

            return new sun.misc.BASE64Encoder().encode(encrypted);

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decryptAES(String data) throws Exception {
        try {
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data);

            Cipher cipher = Cipher.getInstance("AES/CBC/NOPadding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes("UTF-8"));

            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);

            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original);
            return originalString;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
